package com.corpgovernment.organization.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.corpgovernment.api.organization.model.org.request.InitPayTypeRequest;
import com.corpgovernment.common.enums.PayTypeEnum;
import com.corpgovernment.common.enums.SiteEnum;
import com.corpgovernment.organization.entity.db.MbOrgPayType;
import com.corpgovernment.organization.mapper.MbOrgPayTypeMapper;
import com.corpgovernment.organization.service.IOrgPayTypeService;
import com.corpgovernment.organization.service.IOrganizationInfoService;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 支付方式service实现类
 *
 * @author wangyujue
 */
@Service
@Slf4j
public class OrgPayTypeServiceImpl implements IOrgPayTypeService {

    @Autowired
    private MbOrgPayTypeMapper mbOrgPayTypeMapper;
    @Autowired
    private IOrganizationInfoService organizationInfoService;

    @Override
    public boolean save(MbOrgPayType mbOrgPayType) {
        int result = mbOrgPayTypeMapper.save(mbOrgPayType);
        return result > 0;
    }

    @Override
    public void batchSavePayType(List<MbOrgPayType> list) {
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        mbOrgPayTypeMapper.insertList(list.stream().peek(payType->payType.setIsDeleted(0)).collect(Collectors.toList()));
    }

    @Override
    public boolean batchUpdate(List<String> orgIds, String payType, String site) {
        int result = mbOrgPayTypeMapper.batchUpdate(orgIds, payType, site);
        return result > 0;
    }

    @Override
    public List<MbOrgPayType> findByOrgId(String orgId, String site) {
        return mbOrgPayTypeMapper.findByOrgId(orgId, site);
    }

    @Override
    public List<MbOrgPayType> findPayTypeList(String orgId, String site, String corpPayType) {
        return mbOrgPayTypeMapper.findPayTypeList(orgId, site, corpPayType);
    }

    @Override
    public List<MbOrgPayType> findByOrdId(String orgId) {
        return mbOrgPayTypeMapper.selectByOrgId(orgId);
    }

    @Override
    public List<MbOrgPayType> findOrgPayTypeByOrgId(String orgId) {
        List<MbOrgPayType> payTypeList = mbOrgPayTypeMapper.findOrgPayTypeByOrgId(orgId);
        log.info("orgId:{},payTypeList:{}", orgId, JsonUtils.toJsonString(payTypeList));
        return payTypeList;
    }

    @Override
    public List<MbOrgPayType> listByOrgIds(List<String> orgIds, String site, String paytype, String supplierId) {
        return mbOrgPayTypeMapper.listByOrgIds(orgIds, site, paytype, supplierId);
    }

    @Override
    public boolean setPPayByOrgIds(List<String> orgIds, String type, String site, String supplierId) {
        return mbOrgPayTypeMapper.deleteByOrgIds(orgIds, type, site, supplierId) > 0;
    }

    @Override
    public boolean delete(List<String> orgIds, String type, String site, String supplierId, boolean updateDeleteStatus) {

        orgIds = Optional.ofNullable(orgIds).orElse(new ArrayList<>()).stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(orgIds)) {
            return false;
        }
        
        return mbOrgPayTypeMapper.setDeleteByOrgIds(orgIds, type, site, supplierId) > 0;
    }

    @Override
    public boolean deletePayType(String orgId, String corpPayType, String payType, String site) {
        return mbOrgPayTypeMapper.deletePayType(orgId,corpPayType,payType,site) > 0;
    }

    @Override
    public void initPayType(InitPayTypeRequest initPayTypeRequest) {
        Boolean ifNeedThroughOrg = initPayTypeRequest.getIfNeedThroughOrg();
        String orgId = initPayTypeRequest.getOrgId();
        PayTypeEnum payType = initPayTypeRequest.getPayType();
        SiteEnum site = initPayTypeRequest.getSite();
        String supplierCode = initPayTypeRequest.getSupplierCode();
        List<String> orgIds = CollUtil.newArrayList(orgId);
        // 是否需要穿透
        if (ifNeedThroughOrg) {
            CollUtil.addAll(orgIds, organizationInfoService.listAllDepartmentByOrgId(orgId));
            CollUtil.distinct(orgIds);
        }
        // 保存支付方式
        for (String id : orgIds) {
            MbOrgPayType mbOrgPayType = new MbOrgPayType();
            mbOrgPayType.setSuppliercode(supplierCode);
            mbOrgPayType.setPaytype(payType.getType());
            mbOrgPayType.setSite(site.getCode());
            mbOrgPayType.setOrgId(id);
            save(mbOrgPayType);
        }
    }
}
